package org.dreamfly.core.page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PageBean<T> implements Serializable {
	int currentPage = 1; // 当前页
	public int totalPages = 0; // 总页数
	int pageRecorders = 10;// 每页10条数据
	int totalRows = 0; // 总数据数
	int pageStartRow = 0;// 每页的起始数
	int pageEndRow = 0; // 每页显示数据的终止数
	boolean hasNextPage = false; // 是否有下一页
	boolean hasPreviousPage = false; // 是否有前一页
	ArrayList arrayList;//需要分页显示的集合
	Iterator it;

	public PageBean() {
	}

	public PageBean(ArrayList arrayList) {
		this.arrayList = arrayList;
		totalRows = arrayList.size();
		it = arrayList.iterator();
		hasPreviousPage = false;
		currentPage = 1;
		if ((totalRows % pageRecorders) == 0) {
			totalPages = totalRows / pageRecorders;
		} else {
			totalPages = totalRows / pageRecorders + 1;
		}

		if (currentPage >= totalPages) {
			hasNextPage = false;
		} else {
			hasNextPage = true;
		}

		if (totalRows < pageRecorders) {
			this.pageStartRow = 0;
			this.pageEndRow = totalRows;
		} else {
			this.pageStartRow = 0;
			this.pageEndRow = pageRecorders;
		}

	}

	public List<T> getNextPage() {
		currentPage = currentPage + 1;
		System.out.println("PageBean.getNextPage()正在执行；");
		System.out.println("参数currentPage=" + currentPage);
		if ((currentPage - 1) > 0) {
			hasPreviousPage = true;
		} else {
			hasPreviousPage = false;
		}
		if (currentPage >= totalPages) {
			hasNextPage = false;
		} else {
			hasNextPage = true;
		}
		System.out.println("参数hasNextPage=" + hasNextPage);
		System.out.println("准备执行PageBean.getT");
		List<T> result = getT();

		return result;
	}

	public List<T> getPreviouspage() {
		currentPage = currentPage - 1;
		if (currentPage == 0) {
			currentPage = 1;
		}
		if (currentPage >= totalPages) {
			hasNextPage = false;
		} else {
			hasNextPage = true;
		}
		if ((currentPage - 1) > 0) {
			hasPreviousPage = true;
		} else {
			hasPreviousPage = false;
		}
		List<T> result = getT();
		return result;
	}

	public List<T> getT() {
		System.out.println("pageBean.getBooks（）开始执行；");

		if (currentPage * pageRecorders < totalRows) {// 判断是否为最后一页
			pageEndRow = currentPage * pageRecorders;
			pageStartRow = pageEndRow - pageRecorders;
		} else {
			pageEndRow = totalRows;
			pageStartRow = pageRecorders * (totalPages - 1);
		}
		List<T> result = new ArrayList<T>(pageEndRow - pageStartRow + 1);

		System.out.println("pageStartRow=" + pageStartRow);
		System.out.println("pageEndRow=" + pageEndRow);
		int j = 0;
		for (int i = pageStartRow; i < pageEndRow; i++) {
			T obj = (T) arrayList.get(i);
			result.add(j++, obj);
		}
		return result;
	}

	/*
	 * Getters and Setters
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public int getPageRecorders() {
		return pageRecorders;
	}

	public void setPageRecorders(int pageRecorders) {
		this.pageRecorders = pageRecorders;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getPageStartRow() {
		return pageStartRow;
	}

	public void setPageStartRow(int pageStartRow) {
		this.pageStartRow = pageStartRow;
	}

	public int getPageEndRow() {
		return pageEndRow;
	}

	public void setPageEndRow(int pageEndRow) {
		this.pageEndRow = pageEndRow;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public ArrayList getArrayList() {
		return arrayList;
	}

	public void setArrayList(ArrayList arrayList) {
		this.arrayList = arrayList;
	}

	public Iterator getIt() {
		return it;
	}

	public void setIt(Iterator it) {
		this.it = it;
	}
}
